import { Component, OnInit, ViewChild } from '@angular/core';
import { _HttpClient, ModalHelper } from '@delon/theme';
import {STColumn, STComponent, STData} from '@delon/abc';
import { SFSchema } from '@delon/form';
import {ActModelParamComponent} from "../model/param/param.component";
import {ActModelVersionComponent} from "../model/version/version.component";
import {ActModelImageComponent} from "../model/image/image.component";
import {ActRuntimeImageComponent} from "../runtime/image/image.component";

@Component({
  selector: 'app-act-process',
  templateUrl: './process.component.html',
})
export class ActProcessComponent implements OnInit {
  url = `act/Definition/list`;
  searchSchema: SFSchema = {
    properties: {
      no: {
        type: 'string',
        title: '编号'
      }
    }
  };
  @ViewChild('st', { static: false }) st: STComponent;
  columns: STColumn[] = [
    { title: 'ID', index: 'id' },
    { title: '键值', index: 'key' },
    { title: '名称', index: 'name' },
    { title: '描述', index: 'description' },
    { title: '版本', index: 'version' },
    {
      title: '操作',
      buttons: [
        {
          text: `删除`,
          type: 'del',
          click: (record) => this.http.delete(`act/Definition/delete?id=${record.deploymentId}`).subscribe(res => this.st.reload())
        },
        {
          text: '配置流程', type: 'modal',
          modal: {
            component: ActModelParamComponent,
            size:"xl"
          },
          click: 'reload'
        },
        {
          text: '版本管理', type: 'modal',
          modal: {
            component: ActModelVersionComponent,
            size:"xl"
          },
          click: 'reload'
        },
        {
          text: `激活`,
          type: 'none',
          click: (record) => this.http.post(`act/Definition/activateProcessDefinitionById?id=${record.id}`).subscribe(res => this.st.reload()),
          iif: (item: STData) => {
            return item.suspensionState !== 1;
          },
        },
        {
          text: `启动流程`,
          type: 'none',
          click: (record) => this.http.post(`act/processInstance/startProcessInstance?key=${record.key}`,{}).subscribe(res => this.st.reload()),
          iif: (item: STData) => {
            return item.suspensionState !== 2;
          },
        },
        {
          text: `挂起`,
          type: 'none',
          click: (record) => this.http.post(`act/Definition/suspendProcessDefinitionById?id=${record.id}`).subscribe(res => this.st.reload()),
          iif: (item: STData) => {
            return item.suspensionState !== 2;
          },
        },
        // { text: '编辑', type: 'static', component: FormEditComponent, click: 'reload' },
      ]
    }
  ];

  constructor(private http: _HttpClient, private modal: ModalHelper) { }

  ngOnInit() { }

  add() {
    // this.modal
    //   .createStatic(FormEditComponent, { i: { id: 0 } })
    //   .subscribe(() => this.st.reload());
  }

}
